package com.wing.sell.controller.admin;

import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wing.common.utils.PageResult;


import com.wing.purchase.model.response.PurchaseDetailExportVO;
import com.wing.sell.model.response.FinishedOrderDetailExcelVo;
import com.wing.sell.model.response.FinishedOrderDetailVo;

import com.wing.sell.service.FinishedOrderDetailService;
import com.wing.utils.ExcelUtils;
import com.wing.web.db.config.util.DynamicDataSource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 成品销售明细控制层
 * @author hz
 */
@Validated
@Api(value = "Admin-成品销售明细控制层", tags = "#/sell/finishedOrderDetail")
@RestController("finishedOrderDetailController")
@RequestMapping("sell/admin/finishedDetailOrder")
@RequiredArgsConstructor
@Slf4j
public class FinishedOrderDetailController {

    private final FinishedOrderDetailService finishedOrderDetailService;

    @ApiOperation(value = "查询所有成品销售明细信息", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "sort", value = "排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "order", value = "排序规则(asc:正序, desc:倒序)", defaultValue = "desc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<FinishedOrderDetailVo> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "desc") String order
            , String skuCode
            , String customerId
            , String orderCode
            , String skuName
            , String startTime
            , String endTime
    ) {
        Page<FinishedOrderDetailVo> pg = new Page<>(page, limit);
        //查询条件
        Map<String,Object> params = new HashMap<>();
        params.put("sort", StrUtil.toUnderlineCase(sort));
        params.put("order", StrUtil.toUnderlineCase(order));
        params.put("customerId",customerId);
        params.put("orderCode",orderCode);
        params.put("skuName",skuName);
        params.put("skuCode",skuCode);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        Page<FinishedOrderDetailVo> list = finishedOrderDetailService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam(name = "skuCode", required = false) String skuCode,
                       @RequestParam(name = "skuName", required = false) String skuName,
                       @RequestParam(name = "orderCode", required = false) String orderCode,
                       @RequestParam(name = "startTime", required = false) String startTime,
                       @RequestParam(name = "endTime", required = false) String endTime
    ){
        List<FinishedOrderDetailExcelVo> list = new ArrayList<>();

        Map<String,Object> params = new HashMap();
        if(StringUtils.isNotBlank(skuCode)){
            params.put("skuCode", skuCode);
        }
        if(StringUtils.isNotBlank(skuName)){
            params.put("skuName", skuName);
        }
        if(StringUtils.isNotBlank(orderCode)){
            params.put("orderCode", orderCode);
        }

        String databaseUser = DynamicDataSource.USER_PREFIX;
        if (StringUtils.isNotBlank(DynamicDataSource.name.get())) {
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);

        list = finishedOrderDetailService.exportInProductInfoDetail(params);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String timestamp = sdf.format(new Date());
        // 生成带时间戳的文件名
        String fileName = "成品销售明细表_" + timestamp;
        ExcelUtils.export(response,fileName,list, FinishedOrderDetailExcelVo.class);
    }
}
